
requirejs(['jquery', 'Util', 'Dropzone', 'bootstrap', 'fooTable'], function ($, Util, Dropzone) {

    $(document).ready(function () {

        // Dropzone初始化
        var dropz = new Dropzone("#dropz", {
            //指定上传图片的路径
            url: "/upload/do_upload/",
            //允许上传多个照片
            uploadMultiple: false,
            maxFiles: 10,
            acceptedFiles: "image/*",
            init: function () {
                var _this = this;
                //添加文件事件监听
                this.on("addedfile", function (file) {
                    // 创建一个移除按钮
                    var removeButton = Dropzone.createElement("<button>放弃上传</button>");
                    // 移除按钮点击
                    removeButton.addEventListener("click", function (e) {
                        e.preventDefault();
                        e.stopPropagation();
                        _this.removeFile(file);
                        // If you want to the delete the file on the server as well,
                        // you can do the AJAX request here.
                    });
                    file.previewElement.appendChild(removeButton);
                });

                //上传完成后事件监听
                this.on("complete", function (data) {
                    if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
                        console.log(data.xhr.response);
                        if (!data.xhr.response) {
                            //上传失败
                            Util.Alert(data.name + '上传失败了...', 1);
                        } else {
                            Util.Alert(data.name + '上传成功!', 1)
                        }
                    }
                });

            }
        });

        var $footable = $('#gallery-list-table');

        $footable.footable();

        // 删除按钮点击
        $('.del-tag-btn').on('click', function () {
            var $node = $(this);
            $.post('/admin/ajax_del_gallery', {
                gallery_id: $node.attr('data-galleryid')
            }, function (ret) {
                if (ret) {
                    Util.Alert('删除成功', 1);
                    $node.parents('tr').remove();
                    $footable.trigger('footable_redraw');
                }
            });
        });

        // 开启按钮点击
        $('.open-tag-btn').on('click', function () {
            var $node = $(this);
            $.post('/admin/ajax_open_gallery', {
                gallery_id: $node.attr('data-galleryid')
            }, function (ret) {
                if (ret) {
                    Util.Alert('操作成功', 1);
                    $node.parents('tr')
                            .find('.gallery-status')
                            .html('显示')
                            .removeClass('text-danger')
                            .addClass('text-navy');
                }
            });
        });

        // 关闭按钮点击
        $('.close-tag-btn').on('click', function () {
            var $node = $(this);
            $.post('/admin/ajax_close_gallery', {
                gallery_id: $node.attr('data-galleryid')
            }, function (ret) {
                if (ret) {
                    Util.Alert('操作成功', 1);
                    $node.parents('tr')
                            .find('.gallery-status')
                            .html('隐藏')
                            .removeClass('text-navy')
                            .addClass('text-danger');
                }
            });
        });

    });

});


